<!-- 矿工收益明细 -->
<template>

  <div class="pag" >
    <div class="miner-info">
      <span>UID：{{uid}}</span>
      <span>{{ createTime }} 加入</span>
    </div>
    <div class="tabs">
      <div class="tabs-head">
        <div :class="{'choose': curTab==1}" @click="tabClick(1)">Linux<span>{{ pInfo.linux || 0 }}</span></div>
        <div :class="{'choose': curTab==2}" @click="tabClick(2)">路由器<span>{{ pInfo.router || 0 }}</span></div>
        <div :class="{'choose': curTab==3}" @click="tabClick(3)">客户端<span>{{ pInfo.pc || 0 }}</span></div>
      </div>
      <div class="tabs-body">
        <div :class="{'choose': curTab==1}">
          <div class="profit-list">
            <div class="profit-head">
              <div>节点名称</div>
              <div>昨日分红</div>
              <div>累计分红</div>
            </div>
            <div class="profit-body">
              <cube-scroll :options="options" @pulling-up="onPullup" ref="scroll">
                <div class="profit-items">
                  <div class="profit-item" v-for="item in list" :key="item.uid">
                    <div>{{ item.name }}</div>
                    <div>{{ item.yesterdayCvnt  }}<span>CVNT</span></div>
                    <div>{{ item.totalCvnt  }}<span>CVNT</span></div>
                  </div>
                </div>
              </cube-scroll>
            </div>
          </div>
        </div>
        <div :class="{'choose': curTab==2}">
          <div class="profit-list">
            <div class="profit-head">
              <div>节点名称</div>
              <div>昨日分红</div>
              <div>累计分红</div>
            </div>
            <div class="profit-body">
              <cube-scroll :options="options" @pulling-up="onPullup" ref="scroll2">
                <div class="profit-items">
                  <div class="profit-item" v-for="item in list2" :key="item.uid">
                    <div>{{ item.name }}</div>
                    <div>{{ item.yesterdayCvnt  }}<span>CVNT</span></div>
                    <div>{{ item.totalCvnt  }}<span>CVNT</span></div>
                  </div>
                </div>
              </cube-scroll>
            </div>
          </div>
        </div>
        <div :class="{'choose': curTab==3}">
          <div class="profit-list">
            <div class="profit-head">
              <div>节点名称</div>
              <div>昨日分红</div>
              <div>累计分红</div>
            </div>
            <div class="profit-body">
              <cube-scroll :options="options" @pulling-up="onPullup" ref="scroll3">
                <div class="profit-items">
                  <div class="profit-item" v-for="item in list3" :key="item.uid">
                    <div>{{ item.name }}</div>
                    <div>{{ item.yesterdayCvnt  }}<span>CVNT</span></div>
                    <div>{{ item.totalCvnt  }}<span>CVNT</span></div>
                  </div>
                </div>
              </cube-scroll>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="btns">
      <div class="del-btn" @click="popupVisible=true"></div>
      <div class="setTop-btn" v-show="setTop" @click="setMinerTop(2)"></div>
      <div class="cancelTop-btn" v-show="!setTop" @click="setMinerTop(1)"></div>
    </div>
    <cube-popup type="my-popup" ref="myPopup" v-model="popupVisible" maskClosable>
      <div class="popup-ct" @touchmove.prevent.stop>
        <h4>是否把 <span>{{ nickname }}</span> 移出我的矿工阵列</h4>
        <p>为保护矿工正常挖矿权益<br/>移除矿工阵列 需满足以下2个条件</p>
        <ul>
          <li>1.超级节点已成功邀请400名矿工进行挖矿；</li>
          <li>2.移除的矿工已连续7日贡献为0 。</li>
        </ul>
        <div class="popup-btns">
          <div class="btn" :class="{'disabled': !canRemoveMiner}" @click="removeAMiner">确认移出</div>
          <div class="btn" @click="popupVisible=false">取消移出</div>
        </div>
      </div>
    </cube-popup>
  </div>

</template>

<script>
  import { getMinerNodecount, getMinerProfitList, removeMiner, setMinerTop } from 'api'

  export default {
    data() {
      return {
          createTime: '',
          canRemoveMiner: false,
          nickname: '',
          popupVisible: false,
        pInfo: {},
        setTop: true,
        options: {
          pullUpLoad: true
        },
        list: [],
        list2: [],
          list3: [],
        isLoading: false,
        curTab: 1,
        uid: '',
        token: ''
      }
    },
    beforeRouteEnter (to, from, next) {
        document.title = (to.query.nickname || '') + to.meta.title
        next((vm) => {
            vm.resetState()
            Object.assign(vm, to.query)
          vm.getData()
        })
    },
    mounted(){

    },
    watch: {
      curTab(val){
        this.$nextTick(()=>{
          this.getScrollEl().forceUpdate(true)
        })
      }
    },
    methods: {
        tabClick(tabIndex){
            this.curTab = tabIndex
            let list = this['list' + (tabIndex == 1 ? '' : tabIndex)]
            if(list.length < 50){
                this.getMinerProfitList()
            }
        },
        async setMinerTop(source){
          if(this.isSetMinerToping){
              return
          }
            this.isSetMinerToping = true
            try {
                let {data} = await setMinerTop({
                    minerId: this.uid,
                    source
                })
                let isTop = source == 1 ? false : true
                this.setTop = !isTop
                this.$eventBus.$emit('miner.setTop', isTop)
            }catch (e) {
                this.$warn(e.info)
            }
            this.isSetMinerToping = false
        },
        resetState(){
            Object.assign(this, {
                popupVisible: false,
                list: [],
                list2: [],
                list3: [],
                curTab: 1,
                setTop: true,
            })
        },
       async onPullup(){
        let me = this
        let sc = this.getScrollEl()
           let list = this.getList()
        if(list.length < 50){
            await this.getMinerProfitList()
            this.$nextTick(()=>{
                sc.forceUpdate(true)
            })
            return
        }

        let pagec = Math.floor((list.length - 50) / 50) + 1
        let pagey = (list.length - 50) % 50
           try {
               let {data} = await getMinerProfitList({
                   minerId: this.uid,
                   page: 1 + pagec,
                   per: 50,
                   source: this.curTab
               })
               if(!data){
                   return
               }
               if(pagey){
                   data.splice(0, pagey)
               }
                this.setList(list.concat(data))
               if(!data.length){
                   sc.forceUpdate()
               }else{
                   this.$nextTick(()=>{
                       sc.forceUpdate(true)
                   })
               }
           }catch (e) {
               sc.forceUpdate()
               this.$warn(e.info)
           }

      },
        getScrollEl(){
            return this.$refs['scroll' + (this.curTab == 1 ? '' : this.curTab)]
        },
        getList(){
            if(this.curTab == 1){
                return this.list
            }else{
                return  this['list' + this.curTab]
            }
        },
        setList(list){
            if(this.curTab == 1){
                this.list = list
            }else{
                this['list' + this.curTab] = list
            }
        },
      getData(){
        this.getMinerNodecount()
          this.getMinerProfitList()
      },
        async removeAMiner(){
            try {
                await removeMiner({
                    minerId: this.uid,
                    type: 10
                })
                this.$suc('移除成功!')
                this.popupVisible = false
                this.$router.back();
                this.$eventBus.$emit('miner.deleted')
            }catch (e) {
                this.$warn(e.info)
                //this.$router.back();
                //this.$eventBus.$emit('miner.deleted')
            }
        },
        async getMinerProfitList(){
            try {
                let {data} = await getMinerProfitList({
                    minerId: this.uid,
                    page: 1,
                    per: 50,
                    source: this.curTab
                })
                if(data){
                    if(this.curTab == 1){
                        this.list = data
                    }else{
                        this['list' + this.curTab] = data
                    }
                }
                return data
            }catch (e) {
                this.$warn(e.info)
            }
        },
      async getMinerNodecount(){
        try {
            let {data} = await getMinerNodecount({
                minerId: this.uid
            })
            this.pInfo = data
            return data
        }catch (e) {
            this.$warn(e.info)
        }
      },
      toastTip (info) {
        this.$warn(info)
      },
    }
  }
</script>

<style lang="scss" scoped>
  @import "~common/scss/variable";
  @import "~common/scss/mixin";

  .pag {
    height: 100%;
    overflow-y: auto;
    padding: 18px;
    background: #fff;
  }

  .miner-info {
    height: 34px;
    font-size: 24px;
    color: #333;
    margin-bottom: 12px;
    display: flex;
    justify-content: space-between;
    span {
      &:nth-of-type(2) {
        opacity: .3;
      }
    }
  }

  .tabs {
    height: calc(100% - 210px);
  }

  .tabs-head {
    display: flex;
    height:86px;
    margin-bottom: -10px;
    > div {
      width: auto;
      flex: 1;
      display: flex;
      align-items: center;
      justify-content: center;
      color: rgba(51,51,51,.3);
      font-size: 36px;
      font-weight: 600;
      &.choose {
        background: #5D5D5D;
        color: #fff;
        border-radius: 8px;
        span {
          background: #FF6969;
        }
      }
      span {
        min-width:40px;
        padding: 0 6px;
        height:32px;
        background:rgba(204,204,204,1);
        border-radius:4px;
        color: #fff;
        font-size: 28px;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-left: 12px;
        &:empty {
          display: none;
        }
      }
    }
  }

  .tabs-body {
    height: calc(100% - 86px);
    //flex: 1;
    background: #5D5D5D;
    padding: 10px;
    border-radius: 8px;
    > div {
      height: 100%;
      display: none;
      border-radius: 8px;
      background: #fff;
      &.choose {
        display: block;
      }
    }
  }

  .profit-list {
    position: relative;
    height: 100%;
  }

  .profit-head {
    position: sticky;
    top: -1px;
    height: 74px;
    border-bottom: 2px solid rgba(151,151,151, .1);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 24px;
    color: #000;
    font-size: 32px;
    font-weight: 500;
  }

  .profit-body {
    height: calc(100% - 74px);
    overflow: auto;
    color: #666;
  }

  .profit-items {

  }

  .profit-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 74px;
    padding: 0 14px;
    font-size: 28px;
    > div {
      span {
        color: #A8A8A8;
      }
      &:nth-of-type(1) {
        width: 35%;
        text-align: left;
      }
      &:nth-of-type(2) {
        width: 30%;
        text-align: center;
      }
      &:nth-of-type(3) {
        width: 35%;
        text-align: right;
      }
    }
  }

  .btns {
    display: flex;
    justify-content: space-between;
    margin: 22px 0 40px 0;
    > div {
      width: 332px;
      height: 100px;
      background: no-repeat center center;
      background-size: 100% 100%;
    }
    .del-btn {
      background-image: url("~assets/images/miner/del.png");
    }
    .setTop-btn {
      background-image: url("~assets/images/miner/set.png");
    }
    .cancelTop-btn {
      background-image: url("~assets/images/miner/cancel.png");
    }
  }

  .popup-ct {
    width:638px;
    height:533px;
    background:rgba(255,255,255,1);
    border-radius:8px;
    padding-top: 80px;
    font-size:28px;
    text-align: center;
    h4 {
      font-size:36px;
      font-weight:600;
      color:rgba(32,44,91,1);
      margin-bottom: 44px;
      span {
        display: inline-block;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        max-width: 5.5em;
        vertical-align: top;
      }
    }
    p {
      font-weight:400;
      color:rgba(113,118,140,1);
      line-height:40px;
      margin-bottom: 44px;
    }
    ul {
      list-style: none;
      padding: 0 40px;
      outline: none;
      text-align: left;
      li {
        margin-bottom: 30px;
        color:rgba(32,44,91,1);
        line-height:36px;
      }
    }
  }


  .popup-btns {
    margin-top: 55px;
    display: flex;
    justify-content: space-between;
    padding: 0 32px;
    .btn {
      width:272px;
      height:64px;
      background:rgba(23,62,143,1);
      border-radius:30px;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 26px;
      color: #fff;
      &.disabled {
        background:rgba(23,62,143,1);
        opacity:0.5;
        pointer-events: none;
      }
    }
  }

</style>













